package com.diamond.web.controller.system;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.diamond.core.entity.Result;
import com.diamond.web.utils.ApplicationInitUtils;
import com.diamond.web.utils.SK;
import com.diamond.web.controller.GenericController;
import com.diamond.web.model.system.Permission;
import com.diamond.web.model.system.User;
import com.diamond.web.security.RoleSign;
import com.diamond.web.security.SecurityRealm;
import com.diamond.web.service.system.PermissionService;

@RequestMapping("admin/menu")
@Controller
public class MenuController extends GenericController{

	
	@Resource
	PermissionService permissionService;
	
	@RequiresRoles({RoleSign.SUPER_ADMIN})
	@RequestMapping("allParents")
	@ResponseBody
	public List<Permission> allParents(){
		return ApplicationInitUtils.getAllParentList();
	}
	
	@RequiresRoles({RoleSign.SUPER_ADMIN})
	@RequestMapping("list")
	public ModelAndView toList(){
		ModelAndView mv  = getModelAndView("list");
		return mv;
	}
	
	
	@RequiresRoles({RoleSign.SUPER_ADMIN})
	@RequestMapping("edit")
	public ModelAndView toEdit(Long id){
		ModelAndView mv = getModelAndView("save");
		mv.addObject("m", permissionService.selectById(id));
		beforeToAdd(mv);
		return mv;
	}
	
	@RequiresRoles({RoleSign.SUPER_ADMIN})
	@RequestMapping("add")
	public ModelAndView toAdd(){
		ModelAndView mv = getModelAndView("save");
		beforeToAdd(mv);
		return mv;
	}
	
	@RequiresRoles({RoleSign.SUPER_ADMIN})
	@RequestMapping("save")
	@ResponseBody
	public Result editData(@Valid Permission model,BindingResult validResult,Long parentId){
		Result result = new Result();
		if(parentId!=null){
			model.setParent(new Permission(parentId));
		}
		if(model.getId()==null){
			permissionService.insert(model);
		}else{
			permissionService.update(model);
		}
		result.setSuccess(true);
		afterMenuChange();
		return result;
	}
	
	@RequiresRoles({RoleSign.SUPER_ADMIN})
	@RequestMapping("delete")
	@ResponseBody 
	public Result delete(Long id){
		Result result =new Result();
		permissionService.deletePermission(id);
		result.setSuccess(true);
		afterMenuChange();
		return result;
	}
	
	@RequiresRoles({RoleSign.SUPER_ADMIN})
	@RequestMapping("listData")
	@ResponseBody
	public List<Permission> list() {
		return permissionService.selectListByParentId(getLong("parentId"));
	}
	
	
	/**
	 * 添加子权限
	 * @param parentId
	 * @return
	 */
	@RequiresRoles({RoleSign.SUPER_ADMIN})
	@RequestMapping("addSub")
	public ModelAndView addSub(Long parentId){
		ModelAndView mv = getModelAndView("addSub");
		mv.addObject("allUrls",ApplicationInitUtils.getAllUrls());
		mv.addObject("allMenus", ApplicationInitUtils.getAllPermissions());
		return mv;
	}
	
	@RequiresRoles({RoleSign.SUPER_ADMIN})
	@RequestMapping("addSubData")
	@ResponseBody
	public Result addSubData(@Valid Permission model,BindingResult validResult,Long parentId){
		Result result = new Result();
		if(parentId!=null){
			model.setParent(new Permission(parentId));
		}
		permissionService.addSubPermission(model);
		result.setSuccess(true);
		afterMenuChange();
		return result;
	}
	
	@RequiresRoles({RoleSign.SUPER_ADMIN})
	@RequestMapping("editSub")
	public ModelAndView editSub(Long id){
		ModelAndView mv = getModelAndView("addSub");
		mv.addObject("m", permissionService.selectById(id));
		mv.addObject("allUrls",ApplicationInitUtils.getAllUrls());
		mv.addObject("allMenus", ApplicationInitUtils.getAllPermissions());
		return mv;
	}
	
	@RequiresRoles({RoleSign.SUPER_ADMIN})
	@RequestMapping("editSubData")
	@ResponseBody
	public Result editSubData(@Valid Permission model,BindingResult validResult,Long parentId){
		Result result = new Result();
		if(parentId!=null){
			model.setParent(new Permission(parentId));
		}
		permissionService.updateSubPermission(model);
		result.setSuccess(true);
		afterMenuChange();
		return result;
	}
	
	@Resource
	SecurityRealm securityRealm;
	
	public void afterMenuChange() {
		ApplicationInitUtils.initUrlList();
		User u = (User) getSession().getAttribute(SK.USER_INFO);
		SecurityUtils.getSubject().login(new UsernamePasswordToken(u.getUsername(),u.getPassword()));
	}
	
	public void beforeToAdd(ModelAndView mv) {
		mv.addObject("allUrls",ApplicationInitUtils.getAllUrls());
		mv.addObject("parentMenus",ApplicationInitUtils.getAllParentList());
	}
	
}
